import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from hyrobot.common import CHECK_POINT, STEP

from lib.webui import  *

def message_check(driver,css_s):
    time.sleep(1)
    getted1 = False
    error = ""
    # a = self.driver.find_element(By.CSS_SELECTOR, "body > div.el-message.el-message--error > p").text
    while not getted1:
        try:
            error += driver.find_element(By.CSS_SELECTOR,css_s
                                             ).text
        except Exception:
            getted1 = False
            print(False)
            continue
        getted1 = True
        return error


class c73306:
    name = '验证修改密码时的有效字符验证功能-73306' # 测试用例名字

# 初始化方法
    def setup(self):
        STEP(1, '登陆网站')
        wd = open_browser()
        mgr_login(wd)

    #清除方法
    def teardown(self):
        wd = get_global_webdriver()
        wd.quit()

    # 测试用例步骤
    def teststeps(self):
        # 获取webdriver对象 对应 已经登录好的浏览器
        driver = get_global_webdriver()
        # driver = webdriver.Firefox()
        time.sleep(2)
        STEP(2,"修改修改密码")
        driver.find_element(By.CSS_SELECTOR,
                            ".avatar-wrapper > span:nth-child(1)"
                            ).click()
        time.sleep(2)
        driver.find_element(By.CSS_SELECTOR,
                            "li.el-dropdown-menu__item:nth-child(1) > span:nth-child(1)"
                            ).click()
        driver.find_element(By.CSS_SELECTOR, ".el-form-item:nth-child(1) .el-input__inner").send_keys("123456")
        driver.find_element(By.CSS_SELECTOR, ".el-form-item:nth-child(2) .el-input__inner").send_keys("abc123")
        driver.find_element(By.CSS_SELECTOR, ".el-form-item:nth-child(3) .el-input__inner").send_keys("abc123")
        driver.find_element(By.CSS_SELECTOR, ".dialog-footer > .el-button--primary > span").click()
        time.sleep(3)
        success_msg = message_check(driver,
                                    "body > div.el-notification.right > div > div.el-notification__content > p"
                                    )
        CHECK_POINT("提示信息是否一致", success_msg == "修改密码成功")
        time.sleep(6)
        driver.find_element(By.CSS_SELECTOR,
                            ".avatar-wrapper > span:nth-child(1)"
                            ).click()
        time.sleep(2)
        driver.find_element(By.CSS_SELECTOR, ".el-dropdown-menu__item:nth-child(2) > span").click()

        STEP(3, '输入此账号和修改前密码，点击登录')
        driver.find_element(By.NAME, "username").send_keys('user4')
        driver.find_element(By.NAME, "password").send_keys('123456')
        driver.find_element(By.CSS_SELECTOR, ".el-button").click()
        login_error = message_check(driver,"body > div.el-message.el-message--error > p")
        # 用后等一下
        time.sleep(1)

        expeted_error_psw_msg = "用户名或密码错误"
        CHECK_POINT("验证是否为期待文字", login_error == expeted_error_psw_msg)

        STEP(4,"清空后重新输入新密码")
        # try:
        #     driver.find_element(By.NAME, "username").clear()
        #     driver.find_element(By.NAME, "password").clear()
        # except:
        #     print("clear fault")
        new_driver = webdriver.Firefox()
        new_driver.get("http://172.17.66.19:8080/")
        # driver.find_element(By.NAME, "username").click()
        new_driver.find_element(By.NAME, "username").send_keys("user4")
        new_driver.find_element(By.NAME, "password").send_keys("abc123")
        new_driver.find_element(By.NAME, "password").send_keys(Keys.ENTER)
        # expetced_words1 = "产品列表"
        # key_words1 = new_driver.find_element(By.CSS_SELECTOR,
        # ".no-redirect"
        #                                      ).text
        # CHECK_POINT("验证是否为期待文字", expetced_words1 == key_words1)
        time.sleep(2)
        STEP(5, "还原密码")
        new_driver.find_element(By.CSS_SELECTOR,
                            ".avatar-wrapper > span:nth-child(1)"
                            ).click()
        time.sleep(2)
        new_driver.find_element(By.CSS_SELECTOR,
                            "li.el-dropdown-menu__item:nth-child(1) > span:nth-child(1)"
                            ).click()
        new_driver.find_element(By.CSS_SELECTOR, ".el-form-item:nth-child(1) .el-input__inner").send_keys("abc123")
        new_driver.find_element(By.CSS_SELECTOR, ".el-form-item:nth-child(2) .el-input__inner").send_keys("123456")
        new_driver.find_element(By.CSS_SELECTOR, ".el-form-item:nth-child(3) .el-input__inner").send_keys("123456")
        new_driver.find_element(By.CSS_SELECTOR, ".dialog-footer > .el-button--primary > span").click()

        new_driver.quit()
